const fs = require('fs')

const path = require('path')

const http = require('http')

const server = http.createServer()

server.on('request',(req,res)=>{
   const url = req.url

   /**
    * 有以下两个需求因此需要优化请求路径
    * 1.希望请求/时直接能跳到index.html
    * 2.如果手动请求index.html时不想再写hello这一层目录
    * 
   */
   let fpath = ''

   if(req.url === '/'){
      fpath = path.join(__dirname,'/hello小案例/index.html')
   }else{
      fpath = path.join(__dirname,'/hello小案例/',url)
   }

   res.setHeader('Content-Type','text/html;charset=utf-8')

   fs.readFile(fpath,'utf-8',(err,dataStr)=>{
       if(err){
          res.end('404 not found！');
       }
       res.end(dataStr)
   })

})

server.listen(8080,()=>{
   console.log('Server is running at http://127.0.0.1:8080');
})

